// pages/shop/shop.js
Page({
 /**
   * 页面的初始数据
   */
  data: {    
    carts: [], //数据 
    iscart: false,    
    hIDden: null,    
    isAllSelect: false,    
    totalMoney: 0,
    miaoshu:"",
    jiage:"",
    imageSrc:""
  },
onLoad:function(options){
    this.setData({
        imageSrc:options.imageSrc,
        jiage:options.jiage,
        miaoshu:options.miaoshu
    })
},
onShow: function () {    
    // 获取产品展示页保存的缓存数据（购物车的缓存数组，没有数据，则赋予一个空数组）  
    var arr = getApp()    
    console.info("缓存数据："+arr);    // 有数据的话，就遍历数据，计算总金额 和 总数量  
    if (arr.length > 0) {      
        // 更新数据  
      this.setData({        
        carts: arr,        
        iscart: true,        
        hIDden: false
      });      
        console.info("缓存数据：" + this.data.carts);
    }else{      
        this.setData({        
            iscart: false,        
            hIDden: true,
      });
    }
  },
  //勾选事件处理函数  
  switchSelect: function (e) {    
    // 获取item项的ID，和数组的下标值  
    var Allprice = 0, i = 0;    
    let ID = e.target.dataset.ID,
 
      index = parseInt(e.target.dataset.index);    
    this.data.carts[index].isSelect = !this.data.carts[index].isSelect;    //价钱统计
    if (this.data.carts[index].isSelect) {      
        this.data.totalMoney = this.data.totalMoney + (this.data.carts[index].price * this.data.carts[index].count);
    }    else {      
        this.data.totalMoney = this.data.totalMoney - (this.data.carts[index].price * this.data.carts[index].count);
    }    
    //是否全选判断
    for (i = 0; i < this.data.carts.length; i++) {
      Allprice = Allprice + (this.data.carts[index].price * this.data.carts[index].count);
    }    
    if (Allprice == this.data.totalMoney) {      
        this.data.isAllSelect = true;
    }    else {      
        this.data.isAllSelect = false;
    }    
    this.setData({      
        carts: this.data.carts,      
        totalMoney: this.data.totalMoney,      
        isAllSelect: this.data.isAllSelect,
    })
  },  
    //全选
  allSelect: function (e) {   
     //处理全选逻辑
    let i = 0;    
     if (!this.data.isAllSelect) {      
         this.data.totalMoney = 0;      
         for (i = 0; i < this.data.carts.length; i++) {        
             this.data.carts[i].isSelect = true;        
             this.data.totalMoney = this.data.totalMoney + (this.data.carts[i].price * this.data.carts[i].count);
 
      }
    }    else {      
        for (i = 0; i < this.data.carts.length; i++) {        
            this.data.carts[i].isSelect = false;
      }      
            this.data.totalMoney = 0;
    }    
            this.setData({      
                carts: this.data.carts,      
                isAllSelect: !this.data.isAllSelect,      
                totalMoney: this.data.totalMoney,
    })
  },  // 去结算
  toBuy() {
    wx.showToast({      
       Title: '去结算',      
       icon: 'success',      
       duration: 3000
    });    
    this.setData({      
       showDialog: !this.data.showDialog
    });
  },  //数量变化处理
  handleQuantityChange(e) {    
      var componentID = e.componentID;    
      var quantity = e.quantity;    
      this.data.carts[componentID].count.quantity = quantity;    
      this.setData({      
         carts: this.data.carts,
    });
  },  
   /* 减数 */
  delCount: function (e) {    
   var index = e.target.dataset.index;    
   console.log("刚刚您点击了加一");    
   var count = this.data.carts[index].count;    // 商品总数量-1
    if (count > 1) {      
       this.data.carts[index].count--;
    }    
       // 将数值与状态写回  
    this.setData({      
       carts: this.data.carts
    });    
       console.log("carts:" + this.data.carts);    
       this.priceCount();
  },  
       /* 加数 */
  addCount: function (e) {    
       var index = e.target.dataset.index;    
       console.log("刚刚您点击了加+");    
       var count = this.data.carts[index].count;    // 商品总数量+1  
    if (count < 10) {      
       this.data.carts[index].count++;
    }    
       // 将数值与状态写回  
    this.setData({      
       carts: this.data.carts
    });    
       console.log("carts:" + this.data.carts);    
       this.priceCount();
  }, 
  priceCount: function (e) {    
      this.data.totalMoney = 0;    
      for (var i = 0; i < this.data.carts.length; i++) {      
      if (this.data.carts[i].isSelect == true) {        
          this.data.totalMoney = this.data.totalMoney + (this.data.carts[i].price * this.data.carts[i].count);
      }
 
    }    
          this.setData({      
              totalMoney: this.data.totalMoney,
        })
  },  
  /* 删除item */
  delGoods: function (e) {    
      this.data.carts.splice(e.target.ID.substring(3),1);    // 更新data数据对象  
    if (this.data.carts.length > 0) {      
          this.setData({        
              carts: this.data.carts
      })
      wx.setStorageSync('cart', this.data.carts);      
              this.priceCount();
    } else {      
              this.setData({            
                  cart: this.data.carts,        
                  iscart: false,        
                  hIDden: true,
      })
      wx.setStorageSync('cart', []);
    }
  }
})